package com.cloudptt.api.product.func.lbs;

import com.cloudptt.api.models.lbs.LocationData;
import com.cloudptt.api.models.lbs.ReportData;
import com.cloudptt.api.product.config.lbs.LocationConfig;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.lbs.LBSService.LBSBinder;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.db.DBAdapter;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.IBinder;

public class LBSFunc implements IFunc{

	public static String Log_Key = "LBSFunc";
	private Context ctx = null;

	private LBSBroadcast broadcast;
	private LocationConfig localConfig = null;
	private DBAdapter dpOperator = null;
	private ReportData reportData = null;
	private boolean isLogined = false;

	public LBSFunc(Context ct, LocationConfig localConfig, DBAdapter dpOperator) {
		Log.e(Log_Key, "LBSFunc localConfig = " + localConfig + " SDK_INT = " + android.os.Build.VERSION.SDK_INT);
		this.ctx = ct;
		this.isLogined = false;
		this.localConfig = localConfig;
		this.dpOperator = dpOperator;

//		Intent intent = new Intent(ctx, LBSService.class);
//		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//			ctx.startForegroundService(intent);
//		} else {
//			ctx.bindService(intent, conn,
//					Context.BIND_AUTO_CREATE);
//		}
		ctx.getApplicationContext().bindService(
				new Intent(ctx, LBSService.class), conn,
				Context.BIND_AUTO_CREATE);
		broadcast = new LBSBroadcast(ctx);		
	}


	public LBSService lbs = null;
	ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName arg0, IBinder binder) {

			Log.e(Log_Key, " onServiceConnected start lbsservice");
			LBSBinder lbsbinder = (LBSBinder) binder;
			lbs = lbsbinder.getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// 停止服务
			lbs.stopLBS();
			lbs = null;
		}
	};

	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc)
	{
		Log.e(Log_Key, "onLogined mcIdInfo = " + mcIdInfo);
		Log.e(Log_Key, "onLogined isLogined = " + isLogined + " lbs = " + lbs);
		if(lbs != null && !isLogined)
		{
			String mcpttId = mcIdInfo.getMcpttId();
			String saInfo = mcIdInfo.getSaInfo();
			Log.e(Log_Key, "onLogined saInfo = " + saInfo);
			reportData = new ReportData(saInfo);
			Log.e(Log_Key, "onLogined reportData = " + reportData);

			LocationManager lm = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
			if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
				lbs.startLBS(localConfig);
			}
			isLogined = true;
		}
	}
	@Override
	public void onPublishResult(int publishResult)
	{}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn) 
	{
		Log.d(Log_Key, "doNetworkChange isConn = " + isConn);

		if (!isConn) 
		{
//			if(lbs != null)
//			{
//				lbs.stopLBS();
//			}
			if(dpOperator != null)
			{
				dpOperator.cleanAllLogin();
			}
			isLogined = false;
		}
//				else
//				{
//					lbs.startLBS(localConfig);
//				}

	}

	@Override
	public void doExit() 
	{
		Log.d(Log_Key, "doExit");
		if(ctx != null)
		{
			this.ctx.getApplicationContext().unbindService(conn);
		}
		doLogout();

		clearResource();
	}

	@Override
	public void doForceDetach()
	{
		Log.d(Log_Key, "doForceDetach");
		doLogout();
	}
	@Override
	public void doLogout() {
		Log.e(Log_Key, "doLogout");
		if(lbs != null)
		{
			lbs.stopLBS();
		}
		if(dpOperator != null)
		{
			dpOperator.cleanAllLogin();
		}

		//clearResource();

		isLogined = false;
	}

	private void clearResource()
	{
		if(broadcast!=null)
		{
			broadcast.clearResource();	
			broadcast = null;
		}
		if(dpOperator != null)
		{
			dpOperator.cleanAllLogin();
			dpOperator = null;
		}
		isLogined = false;
		this.ctx = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}

	public void startServer(){
		if(lbs != null)
		{
			lbs.startLBS(localConfig);
		}
	}

	public void stopServer(){
		if(lbs != null)
		{
			lbs.stopLBS();
		}
	}

//	@Override
//	public void clearBtData() {
//		// TODO Auto-generated method stub
//		if(lbs != null)
//		{
//			lbs.clearBtData();
//		}
//	}
	public ReportData getReportData()
	{
		if(reportData != null)
		{
			LocationData data = getCurLocation();
			reportData.setLocData(data);
		}
		return reportData;
	}
	public void setFunNumForReportData(String funcNum)
	{
		if(reportData != null)
		{
			reportData.setFuncNum(funcNum);
		}
	}

	public void setTriggerId(String triggerId)
	{
		if(reportData != null)
		{
			reportData.setTriggerId(triggerId);
			reportData.setFuncNum(triggerId);
		}
	}
	public void updateSaInfo(String saInfo)
	{
		Log.d(Log_Key, "updateSaInfo saInfo = " + saInfo);
		if(reportData != null)
		{
			reportData.setSaInfo(saInfo);
		}
	}
	private LocationData getCurLocation()
	{
		if(lbs != null)
		{
			return lbs.getCurLocation();
		}
		return LocationData.getDefaultLocationData();
	}
	
//	public VitalData getCurVitalData()
//	{
//		if(lbs != null)
//		{
//			return lbs.getCurVitalData();
//		}
//		return null;
//	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onKeepLive isconnected = " + isconnected);

//		if(!isconnected)
//		{
//			if(lbs != null)
//			{
//				lbs.stopLBS();
//			}
//			isLogined = false;
//		}
//		else
//		{
//			Log.e(Log_Key, "onKeepLive isLogined = " + isLogined + " lbs = " + lbs);
//			if(lbs != null && !isLogined)
//			{
//				if(dpOperator != null)
//				{
//					dpOperator.cleanAllLogin();
//				}
//
//				LocationManager lm = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
//				if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//					lbs.startLBS(localConfig);
//				}
//			}
////			isLogined = true;
//		}
	}
}
